package com.service.impl;

import com.entity.BookFlat;
import com.entity.BookFlatDetails;
import com.entity.Branch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.BookFlatMapper;
import com.service.BookFlatDetailsService;
import com.service.BookFlatService;
import com.service.BranchService;
import com.until.ConstantUtil;
import com.vo.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ fileName:BookFlatServiceImpl
 * @ description:
 * @ author:dzh
 * @ createTime:2022/5/9 15:00
 * @ version:1.0.0
 */
@Service
public class BookFlatServiceImpl implements BookFlatService {
    @Resource
    private BookFlatMapper bookFlatMapper;
    @Resource
    private BranchService branchService;
    @Resource
    private BookFlatDetailsService bookFlatDetailsService;
    @Override
    public PageInfo<BookFlat> listPass(Page<BookFlat> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return new PageInfo<>(bookFlatMapper.listPass());
    }

    @Override
    public PageInfo<BookFlat> listAudit(Page<BookFlat> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        return new PageInfo<>(bookFlatMapper.listAudit());
    }

    @Override
    public int insertOne(BookFlat bookFlat) {
        bookFlat.setBfDate(new Date());
        bookFlat.setBfStatus(ConstantUtil.storageStatus.ONE);
        bookFlat.setBfAdtStatus(ConstantUtil.auditStatus.AUDITING);
        return bookFlatMapper.insert(bookFlat);
    }

    @Override
    public int insertMore(BookFlat bookFlat) {
        bookFlat.setBfDate(new Date());
        bookFlat.setBfStatus(ConstantUtil.storageStatus.MORE);
        bookFlat.setBfAdtStatus(ConstantUtil.auditStatus.AUDITING);
        return bookFlatMapper.insert(bookFlat);
    }

    @Override
    @Transactional
    public int updatePass(BookFlat bookFlat) {
        List<BookFlatDetails> bookFlatDetails = bookFlatDetailsService.listByBfId(bookFlat.getBfId());
        for (BookFlatDetails bookFlatDetail : bookFlatDetails) {
            Branch branch = branchService.listById(bookFlat.getStoresId(), bookFlatDetail.getBookId());
            branch.setBookQty(branch.getBookQty()-bookFlatDetail.getBookQty());
            branchService.update(branch);
            Branch branch1 = branchService.listById(bookFlat.getStoresNid(), bookFlatDetail.getBookId());
            branch1.setBookQty(branch1.getBookQty()+bookFlatDetail.getBookQty());
            branchService.update(branch1);
        }
        bookFlat.setBfAuditDate(new Date());
        bookFlat.setBfAdtStatus(ConstantUtil.auditStatus.PASS);

        return bookFlatMapper.update(bookFlat);
    }

    @Override
    public int updateReject(BookFlat bookFlat) {
        bookFlat.setBfAuditDate(new Date());
        bookFlat.setBfAdtStatus(ConstantUtil.auditStatus.REJECT);
        return bookFlatMapper.update(bookFlat);
    }
}
